package com.anjiplus.template.gaea.business.modules.data.dataSource.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.anjiplus.template.gaea.business.modules.data.dataSource.controller.dto.DataSourceDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * Created by raodeming on 2021/4/19.
 */
@Slf4j
public class JdbcUtil {

    private static Lock lock = new ReentrantLock();

    private static Lock deleteLock = new ReentrantLock();

    static Map<Long, DruidDataSource> map = new HashMap<>();

    public static DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) {
        if (map.containsKey(dataSource.getId())) {
            return map.get(dataSource.getId());
        } else {
            lock.lock();
            try {
                log.debug(Thread.currentThread().getName() + "获取锁");
                if (!map.containsKey(dataSource.getId())) {
                    DruidDataSource pool = new DruidDataSource();
                    pool.setName(dataSource.getSourceName());
                    pool.setUrl(dataSource.getJdbcUrl());
                    pool.setUsername(dataSource.getUsername());
                    pool.setPassword(dataSource.getPassword());
                    pool.setDriverClassName(dataSource.getDriverName());
                    map.put(dataSource.getId(), pool);
                    log.info("创建连接池成功：{}", dataSource.getJdbcUrl());
                }
                return map.get(dataSource.getId());
            }  finally {
                lock.unlock();
            }
        }
    }

    /**
     * 删除数据库连接池
     * @param id
     */
    public static void removeJdbcConnectionPool(Long id) {
        deleteLock.lock();
        try {
            DruidDataSource pool = map.get(id);
            if (pool != null) {
                map.remove(id);
            }
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            deleteLock.unlock();
        }

    }

    /**
     * 获取连接
     * @param dataSource
     * @return
     * @throws SQLException
     */
    public static Connection getPooledConnection(DataSourceDto dataSource) throws SQLException {
        DruidDataSource pool = getJdbcConnectionPool(dataSource);
        return pool.getConnection();
    }

    /**
     * 测试数据库连接  获取一个连接
     * @param dataSource
     * @return
     * @throws SQLException
     */
    public static Connection getUnPooledConnection(DataSourceDto dataSource) throws SQLException {
        DriverClassUtil.loadDriverClass(dataSource.getDriverName(), dataSource.getJdbcUrl());
        if (StringUtils.isBlank(dataSource.getUsername()) && StringUtils.isBlank(dataSource.getPassword())) {
            return DriverManager.getConnection(dataSource.getJdbcUrl());
        }
        return DriverManager.getConnection(dataSource.getJdbcUrl(),
                dataSource.getUsername(), dataSource.getPassword());
    }



}
